
#include <BScene>
#include <BNode>
#include <BMesh>

#include <BPickItem>
#include <BPicker>
#include "member_BPicker.h"
#include "member_BNode.h"

using namespace BWE;

#define member						(*(member_BPicker*)_ptr)
#define member_allocate()			_ptr = new member_BPicker(this)
#define member_release()			delete (member_BPicker*)_ptr

BPicker::BPicker()
{
	member_allocate();
}
BPicker::BPicker(const BLine& line)
{
	member_allocate();
	this->setLine(line);
}
BPicker::~BPicker()
{
	member_release();
}

void BPicker::setLine(const BLine& line)
{
	member.line = line;
	member.node = 0;
	member.items.clear();
}
const BLine& BPicker::line() const
{
	return member.line;
}

bool BPicker::exclude(BObject* object)
{
	if (object && !member.exclusions.contain(object))
	{
		return member.exclusions.append(object);
	}
	return false;
}
bool BPicker::include(BObject* object)
{
	if (!object)
		return false;
	return member.exclusions.remove(object);
}

int BPicker::exclusionCount() const
{
	return member.exclusions.size();
}
BObject* BPicker::exclusion(int index) const
{
	return member.exclusions(index);
}

void BPicker::clearExclusions()
{
	member.exclusions.clear();
}

const BPickItem* BPicker::apply(BScene* scene)
{
	member.node = 0;
	member.items.clear();
	if (!scene)
		return 0;
	for (int i = 0; i < scene->nodeCount(); i++)
	{
		BNode* node = scene->node(i);
		member.pick(member.line, node);
	}
	for (int i = 0; i < scene->shapeCount(); i++)
	{
		BShape* shape = scene->shape(i);
		member.pick(member.line, shape);
	}
	return member.items.first();
}
const BPickItem* BPicker::apply(BNode* node)
{
	member.node = 0;
	member.items.clear();
	if (!node)
		return 0;
	member.pick(member.line, node);
	return member.items.first();
}
const BPickItem* BPicker::apply(BShape* shape)
{
	member.node = 0;
	member.items.clear();
	if (!shape)
		return 0;
	member.pick(member.line, shape);
	return member.items.first();
}

int BPicker::itemCount() const
{
	return member.items.size();
}
const BPickItem* BPicker::item(int index) const
{
	return member.items(index);
}

